﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Commonlib;

namespace AlbonWeb.Order
{
    public partial class ItemStyle : System.Web.UI.Page
    {
        private readonly BLL.ItemBLL itembll = new BLL.ItemBLL();
        private readonly BLL.ItemStyleBLL itemstylebll = new BLL.ItemStyleBLL();
        private readonly string orderitemid = HttpContext.Current.Request["orderitemid"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["itemid"] != null)
                {
                    string stylelist = itembll.getStyleList(Request["itemid"].ToString());
                    string cmdstr = "select * from Style where active=1 and ID in (" + stylelist + ")";
                    bindRep(cmdstr, repItem);
                }
                DataTable dt = new DataTable();
                if (Request["copyitemid"] != null)
                {
                    dt = itemstylebll.getItemStyle(Request["copyitemid"].ToString());
                }
                else {
                    dt = itemstylebll.getItemStyle(orderitemid);
                }
                initData(dt);

            }
        }

        //绑定Repeater数据
        protected void bindRep(string cmdstr,Repeater rep)
        {
            SqlHelper.BindRepeater(rep, SqlHelper.SqlConnectionString, CommandType.Text,cmdstr, null);
        }
        

        //绑定款式的样式明细
        protected void repItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string parentid = ((HiddenField)e.Item.FindControl("styleid")).Value;
                //绑定模板
                DropDownList ddlModel = (DropDownList)e.Item.FindControl("ddlModel");
                BLL.ModelBLL modelbll = new BLL.ModelBLL();
                ddlModel.DataSource = modelbll.getModel(User.Identity.Name, parentid);
                ddlModel.DataTextField = "modelName";
                ddlModel.DataValueField = "ID";
                ddlModel.DataBind();
                ddlModel.Items.Insert(0, new ListItem("-select model-","0"));

                
                Repeater repStyle = (Repeater)e.Item.FindControl("repStyle");
                string cmdstr = "select * from Style where active=1 and parentID=" + parentid + "order by sequence asc";
                bindRep(cmdstr, repStyle);
            }
        }

        //初始化已选择的样式 
        protected void initData(DataTable dt)
        {
            string keyvalue = "";
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //根据ID重新从Style表中读取数据，如果样式信息被改动过，则显示更改后的styleName
                string valueid = dt.Rows[i]["valueID"].ToString();
                string value = "";
                if (valueid != "-1")
                {
                    string cmdstr = "select styleName from Style where ID in (" + valueid + ")";
                    DataTable dtstyle = SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
                    if (dtstyle.Rows.Count != 0)
                    {
                        for (int j = 0; j < dtstyle.Rows.Count; j++)
                        {
                            value += dtstyle.Rows[j]["styleName"].ToString() + ",";
                        }
                        
                    }
                }
                else
                {
                    value += dt.Rows[i]["value"].ToString() + ",";
                }
                value = value.Substring(0, value.Length - 1);
                keyvalue += dt.Rows[i]["keyID"].ToString() + "="+valueid+"="+ value + "$";
            }
            if (keyvalue.Length != 0)
            {
                keyvalue = keyvalue.Substring(0, keyvalue.Length - 1);
            }
            hfKeyValue.Value = keyvalue;
            //初始化样式图片和备注信息
            foreach (RepeaterItem ri in repItem.Items)
            {
                TextBox remark = (TextBox)ri.FindControl("remark");
                HiddenField styleid = (HiddenField)ri.FindControl("styleid");
                HiddenField filepath = (HiddenField)ri.FindControl("filepath");
                DataTable dtRemark=itemstylebll.getStyleRemark(orderitemid,styleid.Value);
                if (dtRemark.Rows.Count > 0)
                {
                    remark.Text = dtRemark.Rows[0]["remark"].ToString();
                    filepath.Value = dtRemark.Rows[0]["imgPath"].ToString();
                }
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            BLL.OrderItemBLL orderitembll = new BLL.OrderItemBLL();
            //样式信息ItemStyle
            Albon.Model.ItemStyle itemstyle = new Albon.Model.ItemStyle();
            itemstyle.orderItemID = int.Parse(orderitemid);
            //清除额外布料信息，用于删除已取消的样式的相关布料
            BLL.ItemClothBLL itemclothbll = new BLL.ItemClothBLL();
            itemclothbll.clearOptionCloth(orderitemid);
            string[] keyvalue = hfKeyValue.Value.Split('$');
            for (int j = 0; j < keyvalue.Length; j++)
            {
                string[] value = keyvalue[j].Split('=');
                itemstyle.keyID = int.Parse(value[0]);
                itemstyle.valueID = value[1];
                itemstyle.Value = value[2];
                //添加额外布料信息
                BLL.StyleBLL stylebll = new BLL.StyleBLL();
                stylebll.optionCloth(value[1], orderitemid);
                //样式是否需要额外收费

                //添加样式信息
                itemstylebll.addItemStyle(itemstyle);
            }
            //添加备注及图片信息
            Albon.Model.StyleRemark styleremark = new Albon.Model.StyleRemark();
            styleremark.orderItemID =int.Parse(orderitemid);
            foreach (RepeaterItem ri in repItem.Items)
            {
                FileUpload fileupload =(FileUpload)ri.FindControl("FileUpload1");
                HiddenField filepath = (HiddenField)ri.FindControl("filepath");
                string filename = DateTime.Now.ToString("yyyyMMddhhmmss")+ Path.GetExtension(fileupload.FileName);
                filepath.Value="/Order/Style Picture/"+filename;
                fileupload.SaveAs(Server.MapPath("~") + filepath);
                TextBox remark = (TextBox)ri.FindControl("remark");
                HiddenField styleid = (HiddenField)ri.FindControl("styleid");
                styleremark.styleID = int.Parse(styleid.Value);
                styleremark.remark = remark.Text.Trim();
                styleremark.imgPath = filepath.Value;
                BLL.ItemStyleBLL itemstylebll = new BLL.ItemStyleBLL();
                itemstylebll.addRemark(styleremark);
            }
            //跳转到布料选择页面
            Response.Redirect("/Order/ItemCloth.aspx?orderitemid=" + Request["orderitemid"].ToString());
        }
    }
}